package cn.bone.motan.demo1.web;

import cn.bone.motan.demo1.api.Hello;
import cn.bone.motan.demo1.bean.Motan;
import cn.bone.motan.demo1.dao.MotanDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;

/**
 * Created by superbone on 2017/3/9.
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    private Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Resource
    private Hello hello;
    @Resource
    private MotanDao motanDao;

    @Resource(name="businessAsync")
    private ThreadPoolTaskExecutor executor;

    @RequestMapping("/say")
    public String sayHello(String name){

        String result = hello.sayHello(name);
        return result;
    }

    @RequestMapping("/compute")
    public int compute(int a,int b){

        int result = hello.compute(a,b);
        return result;
    }

    @RequestMapping("test")
    public void test(){

        Random random = new Random();
        for(int i=0;i<100;i++){
            new Thread(new HelloThread(i,hello,motanDao)).start();
            //executor.execute(new HelloThread(i,hello,motanDao));
            /*executor.execute(()->{
                while(true){
                    long start = System.currentTimeMillis();
                    int code = 0;
                    Motan motan = new Motan();
                    motan.setCreateTime(new Date());
                    try{
                        int a = random.nextInt(100);
                        int b = random.nextInt(100);
                        int c = hello.compute(a,b);

                    }catch (Exception e){
                        code = 1;
                        motan.setExceptionClass(e.getClass().getName());
                        motan.setExceptionMessage(e.getMessage());
                    }
                    long end = System.currentTimeMillis();
                    motan.setCode(code);
                    motan.setTimeConsume(end-start);
                    motanDao.add(motan);
                }

            });*/
        }
    }
}
